<?php
class SQLStatement {
	var $WhereClause;
	var $NameAndValues = array();
	var $tableName;
	var $OrderBy;
	var $Limit;
	function SQLStatement() {
	}
	function _insert() {
		$sql = "INSERT INTO $this->tableName (";
		foreach($this->NameAndValues as $prop=>$val) {
			$sql .= "`$prop`, ";
		}
		$sql = substr($sql, 0, (count($sql)-3));
		$sql .= ") VALUES (";
		foreach($this->NameAndValues as $prop=>$val) {
			$sql .= "'$val', ";
		}
		$sql = substr($sql, 0, (count($sql)-3));
		$sql .= ")";
		return $sql;
	}
	function _update() {
		$sql = "UPDATE $this->tableName SET ";
		foreach($this->NameAndValues as $prop=>$val) {
			$sql .= "`$prop` = '$val', ";
		}
		$sql = substr($sql, 0, (count($sql)-3));
		if($this->WhereClause != "")
			$sql .= " WHERE ". $this->WhereClause;
		return $sql;
	}
	function _select() {
		$sql = "SELECT ";
		foreach($this->NameAndValues as $prop=>$val) {
			if($val == "*")
				$sql .= "$val, ";	
			else
				$sql .= "`$val`, ";
		}
		$sql = substr($sql, 0, (count($sql)-3));
		$sql .= " FROM ".$this->tableName;
		if($this->WhereClause != "")
			$sql .= " WHERE ". $this->WhereClause;
		if($this->OrderBy != "")
			$sql .= " ORDER BY " . $this->OrderBy;
		if($this->Limit != "")
			$sql .= " LIMIT " . $this->Limit;
		return $sql;
	}
	function _delete() {
		return "DELETE FROM $this->tableName WHERE $this->WhereClause";
	}
}

class SQLHandler {
	var $sql;
	function SQLHandler($sql) {
		$this->sql = $sql;
	}
	function ExecuteQuery() {
		$r = mysql_query($this->sql) or die(mysql_error());
		if(mysql_num_rows($r)>0)
		{
			while($row = mysql_fetch_array($r)) {
				$array[] = $row;
			}
			mysql_free_result($r);
			return $array;
		}
		else
		{
			mysql_free_result($r);
		}
	}
	function ExecuteUpdate() {
		$r = mysql_query($this->sql) or die(mysql_error());
		if($r)
			return true;
		else
			return false;
		mysql_free_result($r);
	}
}
?>